<?php

namespace app\common\service;

use app\common\exception\AdminApiException;
use app\Request;
use think\Collection;
use think\facade\Db;
use think\helper\Str;
use think\Model;
use Throwable;


/**
 * 服务层基类
 */
class BaseService
{

    /**
     * Model 实例
     * @var Model
     */
    protected $model;

    /**
     * 请求实例
     * @var Request
     */
    protected $request;


    protected $app;


    protected int $site_id;


    /**
     * 不包含前缀的数据表名称
     * @var string
     */
    protected string $table_name;



    public function __construct()
    {
        $this->request = request();
        $this->app = app();
        $this->site_id = intval($this->request->siteId());
    }



    public function getModel()
    {
        return $this->model;
    }


    /**
     * 获取数据表名称
     */
    protected function getTableName()
    {
        if($this->model){
            $this->table_name = Str::snake($this->model->getName());
        }
        return $this->table_name;
    }


    /**
     *
     * @param array $where
     * @param array $order
     * @param string $field
     * @param int $offset
     * @param int $limit
     * @return array|Collection|Model[]
     */
    public function list(array $where = [], array $order = [], string $field = '*', int $offset = 0, int $limit = 10)
    {
        if($this->site_id != 0){
            $where['site_id'] = $this->site_id;
        }
        try {
            $list = $this->model->where($where)->order($order)->field($field)->limit($offset, $limit)->select();
        }catch (Throwable $e){
            throw new AdminApiException($e->getMessage());
        }
        return $list;
    }


    /**
     * 计数统计
     * @param array $where
     * @return int
     */
    public function count(array $where = [])
    {
        if($this->site_id != 0){
            $where['site_id'] = $this->site_id;
        }
        try {
            $count = $this->model->where($where)->count();
        }catch (Throwable $e){
            throw new AdminApiException($e->getMessage());
        }
        return $count;
    }


    /**
     * 详情
     * @param array $where
     * @return array|mixed
     */
    public function info(array $where)
    {
        if($this->site_id != 0){
            $where['site_id'] = $this->site_id;
        }
        try {
            $info = $this->model->where($where)->find();
        }catch (Throwable $e){
            throw new AdminApiException($e->getMessage());
        }
        return $info;
    }


    /**
     * 添加，返回新增ID
     * @param array $data
     * @return mixed
     */
    public function add(array $data)
    {
        if ($this->site_id != 0){
            $data['site_id'] = $this->site_id;
        }
        try {
            $this->model->save($data);
        }catch (Throwable $e){
            throw new AdminApiException($e->getMessage());
        }
        return $this->model->id;
    }


    /**
     * 更新
     * @param array $data
     * @param array $where
     * @return true
     */
    public function update(array $data,array $where)
    {
        if($this->site_id != 0){
            $where['site_id'] = $this->site_id;
        }
        try {
            $this->model->where($where)->save($data);
        }catch (Throwable $e){
            throw new AdminApiException($e->getMessage());
        }
        return true;
    }


    /**
     * 获取所有IDS
     * @param array $where
     * @return string
     */
    public function getIds(array $where,$field): string
    {
        $list = $this->model->where($where)->column($field);
        if(!$list) return '';
        return implode(',',$list);
    }



}